package com.dayuan.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuan.bean.Order;
import com.dayuan.bean.UserInfo;
import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.OrderSelectService;
import com.dayuan.util.StringTime;
import com.dayuan.vo.ResultVo;

@Controller
@RequestMapping("/order")
public class OrderSelectController {

	@Resource(name="orderSelectService")
	private OrderSelectService orderService;

	private static Logger logger = LoggerFactory.getLogger(OrderSelectController.class);

	
	/* 根据用户id或者订单状态查询订单详情表 */
	@RequestMapping(value = "/selectbystate.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selectOrderByState(HttpSession session) {
		ResultVo resultVo = new ResultVo();
		try {
			/*if (state == null) {
				resultVo.setCode(AjaxMsg.FAIL.getValue());
				resultVo.setMsg("参数不正确");
				return resultVo;
			}*/
			UserInfo userinfo =  (UserInfo) session.getAttribute("userinfo");

			List<Order> list = orderService.selectOrderByState(userinfo.getId());
			
			//把时间Date类型转换成String
			for (Order order : list) {
				order.setOrder_timeVo(StringTime.gettime(order.getOrder_time()));
				order.setPay_timeVo(StringTime.gettime(order.getPay_time()));
			}
			
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			resultVo.setData(list);
			resultVo.setMsg("成功");
			return resultVo;

		} catch (Exception e) {
			System.out.println(e);
			logger.error("发生错误：" + e);
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("页面消失在异次元空间");
			return resultVo;
		}

	}

}
